package com.hex.ds.hdtp.core.web.script.analyze.controller;

import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.script.analyze.dto.request.ScriptAnalyzeTaskRequestDto;
import com.hex.ds.hdtp.core.app.script.analyze.service.IScriptAnalyzeTaskService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.script.analyze.converter.IScriptAnalyzeTaskVoConverter;
import com.hex.ds.hdtp.core.web.script.analyze.vo.ScriptAnalyzeTaskVo;
import com.hex.ds.hdtp.core.web.script.analyze.vo.query.ScriptAnalyzeTaskQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Package com.hex.ds.hdtp.core.web.script.analyze.controller
 * @ClassName ScriptAnalyzeTaskController
 * @Description 脚本分析任务
 * @Author gj.xu
 * @Date 2023/12/7 14:31
 * @Version v2.0
 **/
@Slf4j
@RestController
@Api(tags = "003-脚本分析：任务")
@RequestMapping("/api/script/analyze/task")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptAnalyzeTaskController {

    private final IScriptAnalyzeTaskService scriptAnalyzeTaskService;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    private final IScriptAnalyzeTaskVoConverter converter;

    /**
     * @Method queryListByPage
     * @Param
     * @Param scriptAnalyzeTaskQueryVo
     * @Param pageVo
     * @Return org.springframework.http.ResponseEntity
     * @Description 分页查询
     * @Author gj.xu
     * @Date 2023/12/7 15:02
     * @Version V2.0
     */
    @GetMapping
    @ApiOperation("脚本分析-分页查询")
    public ResponseEntity queryListByPage(ScriptAnalyzeTaskQueryVo scriptAnalyzeTaskQueryVo, PageVo pageVo) {
        log.debug("进入【ScriptAnalyzeTaskController.queryListByPage】方法,参数：" + scriptAnalyzeTaskQueryVo.toString());
        ScriptAnalyzeTaskRequestDto requestDto = converter.toScriptAnalyzeTaskRequestDto(scriptAnalyzeTaskQueryVo);
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        return ResponseEntity.ok(scriptAnalyzeTaskService.queryListByPage(requestDto, pageDto));
    }

    /**
     * @Method queryById
     * @Param
     * @Param pkId
     * @Return org.springframework.http.ResponseEntity
     * @Description 根据主键查询
     * @Author gj.xu
     * @Date 2023/12/8 9:28
     * @Version V2.0
     */
    @GetMapping(value = {"/{pkId}"})
    @ApiOperation("脚本分析-根据主键查询")
    public ResponseEntity queryById(@PathVariable("pkId") final String pkId) {
        log.debug("进入【ScriptAnalyzeTaskController.queryById】方法,参数：" + pkId);
        return ResponseEntity.ok(scriptAnalyzeTaskService.queryById(pkId));
    }

    /**
     * @Method add
     * @Param
     * @Param scriptAnalyzeTaskVo
     * @Return org.springframework.http.ResponseEntity
     * @Description 新增脚本分析任务
     * @Author gj.xu
     * @Date 2023/12/8 9:40
     * @Version V2.0
     */
    @PostMapping
    @ApiOperation("新增脚本分析任务")
    public ResponseEntity add(@RequestBody ScriptAnalyzeTaskVo scriptAnalyzeTaskVo) {
        log.info("进入【ScriptAnalyzeTaskController.add】方法: 参数: " + scriptAnalyzeTaskVo.toString());
        return ResponseEntity.ok(scriptAnalyzeTaskService.createTask(converter.toScriptAnalyzeTaskRequestDto(scriptAnalyzeTaskVo)));
    }

    /**
     * @Method delete
     * @Param
     * @Param pkIds
     * @Return org.springframework.http.ResponseEntity
     * @Description 批量删除脚本分析任务
     * @Author gj.xu
     * @Date 2023/12/8 9:27
     * @Version V2.0
     */
    @DeleteMapping
    @ApiOperation("批量删除脚本分析任务")
    public ResponseEntity delete(@RequestBody List<String> pkIds) {
        log.info("进入【ScriptAnalyzeTaskController.delete】方法,参数：" + pkIds.toString());
        scriptAnalyzeTaskService.batchRemoveById(pkIds);
        return ResponseEntity.ok("删除成功");
    }

    /**
     * @Method scriptAnalyze
     * @Param
     * @Param pkId
     * @Return org.springframework.http.ResponseEntity
     * @Description 脚本分析启动
     * @Author gj.xu
     * @Date 2023/12/11 11:31
     * @Version V2.0
     */
    @PutMapping(value = {"/analyze/{pkId}"})
    @ApiOperation("脚本分析")
    public ResponseEntity scriptAnalyze(@PathVariable("pkId") final String pkId) {
        log.info("进入【ScriptAnalyzeTaskController.scriptAnalyze】方法,参数：" + pkId);
        scriptAnalyzeTaskService.scriptAnalyze(pkId);
        return ResponseEntity.ok("脚本分析启动成功");
    }

    /**
     * @Method scriptAnalyzeType
     * @Param
     * @Return org.springframework.http.ResponseEntity
     * @Description 获取脚本转换插件类型
     * @Author gj.xu
     * @Date 2023/12/11 11:33
     * @Version V2.0
     */
    @GetMapping(value = {"/analyze/type"})
    @ApiOperation("脚本分析插件类型")
    public ResponseEntity scriptAnalyzeType() {
        log.debug("进入【ScriptAnalyzeTaskController.scriptAnalyzeType】方法");
        return ResponseEntity.ok(scriptAnalyzeTaskService.scriptAnalyzeType());
    }

    /**
     * @Method upload
     * @Param httpServletRequest
     * @Return org.springframework.http.ResponseEntity
     * @Description 上传脚本
     * @Author gj.xu
     * @Date 2023/12/11 12:45
     * @Version V2.0
     */
    @PostMapping(value = "/upload")
    @ApiOperation("上传脚本")
    public ResponseEntity upload(HttpServletRequest httpServletRequest) {
        log.info("进入【ScriptAnalyzeTaskController.upload】方法");
        List<MultipartFile> multipartFileList = ((MultipartHttpServletRequest) httpServletRequest).getFiles("fileList");
        return ResponseEntity.ok(scriptAnalyzeTaskService.upload(multipartFileList));
    }

    /**
     * @Method createTransferTask
     * @Param scriptAnalyzeTaskVo
     * @Return org.springframework.http.ResponseEntity
     * @Description 创建各迁移任务
     * @Author gj.xu
     * @Date 2023/12/12 17:58
     * @Version V2.0
     */
    @PostMapping(value = {"/createTransferTask"})
    @ApiOperation("创建各迁移任务")
    public ResponseEntity createTransferTask(@RequestBody ScriptAnalyzeTaskVo scriptAnalyzeTaskVo) {
        log.info("进入【ScriptAnalyzeTaskController.createTransferTask】方法: 参数: " + scriptAnalyzeTaskVo.toString());
        scriptAnalyzeTaskService.createTransferTask(converter.toScriptAnalyzeTaskRequestDto(scriptAnalyzeTaskVo));
        return ResponseEntity.ok("创建各迁移任务成功");
    }
}
